<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
    <!-- Bootstrap -->
    <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">

    <!-- easyui -->
    <link rel="stylesheet" type="text/css" href="easyui/jquery-easyui-1.5/themes/gray/easyui.css"/>
    <link rel="stylesheet" type="text/css" href="easyui/jquery-easyui-1.5/themes/icon.css"/>
    <script type="text/javascript" src="easyui/jquery-easyui-1.5/jquery.min.js"></script>
    <script type="text/javascript" src="easyui/jquery-easyui-1.5/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="easyui/jquery-easyui-1.5/locale/easyui-lang-zh_CN.js"></script>

    <!-- easyui扩展 -->
    <link rel="stylesheet" type="text/css" href="easyui/easyui-ext/icons/icon-standard.css"/>
    <script type="text/javascript" src="easyui/easyui-ext/icons/icon-standard.js"></script>

    <!-- 自定义js -->
    <script type="text/javascript" src="js/common.js"></script>
    <script type="text/javascript" src="js/extJs.js"></script>

    <!-- 自定义css -->
    <link rel="stylesheet" href="css/common.css">

    <script type="text/javascript">
        var url;
        function searchUser() {
            $("#dg").datagrid('load', {
                "title" : $("#title").val()

            });
            console.log("--------------"+ $("#title").val());
        }
        function openUserAddDialog() {
            $("#dlg").dialog("open").dialog("setTitle", "添加新闻信息");
            url = "/MyDemo/addXinwen.php?action=add";
        }

        function openUserModifyDialog() {
            var selectedRows = $("#dg").datagrid("getSelections");
            if (selectedRows.length != 1) {
                $.messager.alert("系统提示", "请选择一条要编辑的数据！");
                return;
            }
            var row = selectedRows[0];
            $("#dlg").dialog("open").dialog("setTitle", "编辑新闻信息");
            $("#fm").form("load", row);
            url = "/MyDemo/addXinwen.php?action=update";
        }

        function saveUser() {
            $("#fm").form("submit", {
                url : url,
                onSubmit : function() {
                    // if ($("#title").combobox("getValue") == "") {
                    //     $.messager.alert("系统提示", "请填写信息！");
                    //     return false;
                    // }
                    return $(this).form("validate");
                },
                success : function(result) {
                    if (result==200) {
                        $.messager.alert("系统提示", "添加成功");
                        resetValue();
                        $("#dlg").dialog("close");
                        $("#dg").datagrid("reload");
                    } else {
                        $.messager.alert("系统提示", "添加失败");
                        return;
                    }
                }
            });
        }

        function resetValue() {
            $("#title").val("");
            $("#keywords").val("");
            $("#autor").val("");
            $("#content").val("");
        }

        function closeUserDialog() {
            $("#dlg").dialog("close");
            resetValue();
        }

        function deleteUser() {
            var selectedRows = $("#dg").datagrid("getSelections");
            if (selectedRows.length == 0) {
                $.messager.alert("系统提示", "请选择要删除的数据！");
                return;
            }
            var strIds = [];
            for ( var i = 0; i < selectedRows.length; i++) {
                strIds.push(selectedRows[i].id);
            }
            var ids = strIds.join(",");
            $.messager.confirm("系统提示", "您确定要删除这<font color=red>"
                + selectedRows.length + "</font>条数据吗？", function(r) {
                if (r) {
                    $.post("/MyDemo/addXinwen.php?action=delete", {
                        id : ids
                    }, function(result) {
                        if (result==200) {
                            $.messager.alert("系统提示", "数据已成功删除！");
                            $("#dg").datagrid("reload");
                        } else {
                            $.messager.alert("系统提示", "数据删除失败，请联系系统管理员！");
                        }
                    }, "json");
                }
            });
        }
    </script>
</head>
<body style="margin: 1px">

<table id="dg" title="用户管理" class="easyui-datagrid" fitColumns="true"
       pagination="true" rownumbers="true"
       url="/MyDemo/showList.php" fit="true"
       toolbar="#tb">
    <thead>
    <tr>
        <th field="cb" checkbox="true" align="center"></th>
        <th field="id" width="50" align="center">编号</th>
        <th field="title" width="50" align="center">标题</th>
        <th field="keywords" width="50" align="center">关键字</th>
        <th field="autor" width="50" align="center">作者</th>
        <th field="addtime" width="50" align="center">时间</th>
        <th field="content" width="50" align="center">内容</th>
    </tr>
    </thead>
</table>
<div id="tb">
    <a href="javascript:openUserAddDialog()" class="easyui-linkbutton"
       iconCls="icon-add" plain="true">添加</a> <a
        href="javascript:openUserModifyDialog()" class="easyui-linkbutton"
        iconCls="icon-edit" plain="true">修改</a> <a
        href="javascript:deleteUser()" class="easyui-linkbutton"
        iconCls="icon-remove" plain="true">删除</a>
    <div>
        &nbsp;标题：&nbsp;<input type="text" id="title" size="20"
                               onkeydown="if(event.keyCode == 13)searchUser()" /> <a
            href="javascript:searchUser()" class="easyui-linkbutton"
            iconCls="icon-search" plain="true">查询</a>
    </div>

    <div id="dlg-buttons">
        <a href="javascript:saveUser()" class="easyui-linkbutton"
           iconCls="icon-ok">保存</a> <a href="javascript:closeUserDialog()"
                                       class="easyui-linkbutton" iconCls="icon-cancel">关闭</a>
    </div>

    <div id="dlg" class="easyui-dialog"
         style="width: 730px;height:280px;padding:10px 10px;" closed="true"
         buttons="#dlg-buttons">
        <form method="post" id="fm">
            <table cellspacing="8px;">
                <tr>
                    <input type="hidden" id="id" name="id"/>
                    <td>标题：</td>
                    <td><input type="text" id="title" name="title"
                               class="easyui-validatebox" required="true" />&nbsp;<span
                            style="color: red">*</span>
                    </td>
                    <td>&nbsp;&nbsp;&nbsp;&nbsp;</td>
                    <td>关键词：</td>
                    <td><input type="text" id="keywords" name="keywords"
                               class="easyui-validatebox" required="true" />&nbsp;<span
                            style="color: red">*</span>
                    </td>
                </tr>
                <tr>
                    <td>作者：</td>
                    <td><input type="text" id="autor" name="autor"
                               class="easyui-validatebox" required="true" />&nbsp;<span
                            style="color: red">*</span>
                    </td>
                    <td>&nbsp;&nbsp;&nbsp;&nbsp;</td>
                    <td>内容：</td>
                    <td><input type="text" id="content" name="content"
                                class="easyui-validatebox" required="true" />&nbsp;<span
                            style="color: red">*</span>
                    </td>
                </tr>

            </table>
        </form>
    </div>
</body>
</html>